package leetcode.weekly.week326;

import java.util.ArrayList;
import java.util.List;

//Solution4Test
public class Solution4 {

	public int[] closestPrimes(int left, int right) {
		List<Integer> list = new ArrayList<>();
		for (int i = left; i <= right; i++) {
			if (isPrime(i)) {
				list.add(i);
			}
		}
		if (list.size() < 2) {
			return new int[] { -1, -1 };
		}
		int diff = Integer.MAX_VALUE;
		int index = 0;
		for (int i = 1; i < list.size(); i++) {
			int cur = list.get(i) - list.get(i - 1);
			if (cur < diff) {
				diff = cur;
				index = i - 1;
			}
		}
		return new int[] { list.get(index), list.get(index + 1) };
	}

	public boolean isPrime(int num) {
		if (num == 2) {
			return true;
		}
		if (num == 1 || num % 2 == 0) {
			return false;
		}
		for (int i = 3; i * i <= num; i += 2) {
			if (num % i == 0 || i * i == num) {
				return false;
			}
		}
		return true;
	}

}
